$.fn.zxCombotree = function(options,param){
	let self = this;
	if(typeof options == 'object'){	
		let thisID = self[0].id;
		
		let dropDown = thisID + "dropDownTree";
		let treeDiv = thisID + "treeDiv";
		let optionValue = thisID + "optionValue";
		
		$(self).addClass('dropDpwnTree');
		
		let dropDownArray = $(self).parent().find("#"+ dropDown);
		if(dropDownArray.length > 0){
			$(dropDownArray[0]).html("<div id='" + treeDiv + "' style='height:210px;overflow:auto;'></div>");
		}else{
			$(self).parent().append("<div id='" + dropDown + "' style='display:none;position: absolute;z-index: 99;' class='dropDownTree'><div id='" + 
					treeDiv + "' style='height:210px;overflow:auto;'></div></div>");
		}
		let optionValueArray = $(self).parent().find("#"+ optionValue);
		
		if(dropDownArray.length == 0){
			$(self).parent().append("<input type='hidden' name='"+ optionValue + "' id='"+optionValue+"'/>")
		}
		
		let list = "";
		if(options.data != ""){
			list = zxCombotree_makeTreeData(options.data);
		}
		
		$('#'+treeDiv).treeview({
			data: list,
			showIcon:false,
			expandIcon:"glyphicon glyphicon-triangle-top",
			collapseIcon:"glyphicon glyphicon-triangle-bottom",  
	        onNodeSelected : function(event, data) {
	        	let optionVal = self[0].id + "optionValue";
	        	$("#"+ optionVal).val(data.id);
	            $(self).val(data.text);
	            $("#"+dropDown).hide();  
	        }
		});
		
		$(document).unbind("click",zxCombotree_clickBind);
		
		$(document).bind("click",zxCombotree_clickBind);
		
		$(this).unbind("click");
		
		$(this).click(function (e) {
			let dropDown = $(this)[0].id + "dropDownTree";
			let display =$("#"+dropDown).css('display');
			if(display== 'none'){		
				let w3c=(document.getElementById)? true:false;//w3c 标准  
				let ns6=(w3c && (navigator.appName=="Netscape"))? true: false;//Netscape浏览器 标准的W3C  
				let left,top;  
			    if (!ns6){//判断IE  
			    	let nLt = 0;  
			    	let nTp = 0;  
			    	let offsetParent = this;  
			      
			        while (offsetParent!=null && offsetParent!=document.body) {  
			            nLt += offsetParent.offsetLeft;  
			            nTp += offsetParent.offsetTop;  
			            offsetParent=offsetParent.offsetParent;  
			        }  
			        left = nLt;  
			        top = nTp + this.offsetHeight;  
			    } else {//标准w3c  
			        left = $(this).position().left;  
			        top = $(this).position().top + this.offsetHeight;  
			    }  
			      
			    $("#"+dropDown).css('display','block');  
			    $("#"+dropDown).css('top',top);
			    $("#"+dropDown).css('left',left);
			    $('#'+treeDiv).css('width',this.offsetWidth);
			}else{
				$("#"+dropDown).css('display','none');
			}
		});
	}
	else if(options == "getValue"){
    	let optionVal = self[0].id + "optionValue";
    	return $("#"+ optionVal).val();
	}
	else if(options == "clear"){
		let optionVal = self[0].id + "optionValue";
    	$("#"+ optionVal).val("");
	}

};

function zxCombotree_clickBind(e) {
	let classList = e.target.className;
	for(let i = 0;i <$(".dropDpwnTree").length;i++){
		
		if(classList != "icon expand-icon glyphicon glyphicon-triangle-bottom" 
			&& classList != "icon expand-icon glyphicon glyphicon-triangle-top" 
				&& $(e.target)[0].id != $(".dropDpwnTree")[i].id){
			
			let dropDown = $(".dropDpwnTree")[i].id + "dropDownTree";
			
			let display =$("#"+dropDown).css('display');
			if(display== 'block'){
				$("#"+dropDown).hide();
			}
		}
	}
}

function zxCombotree_makeTreeData(array){
	$.each(array,function(index,data){
		if(data.nodes){			
			if(data.nodes.length > 0){
				zxCombotree_makeTreeData(data.nodes);
			}else{
				delete data.nodes;
			}
		}
	});
	return array;
}